package com.qk.management.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(path = "/users")
public class UserController {
    @Autowired
    private UserService userService;
    @LogAnno
    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        return Result.success();
    }
    @GetMapping
    public Result list(UserDTO dto){
        PageResult<UserDO> pageResult =userService.list(dto);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user =userService.getById(id);
        return Result.success(user);
    }
    @LogAnno
    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }
    @LogAnno
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids){
        boolean isNotEmpty = CollectionUtil.isEmpty(ids);
        if (isNotEmpty){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        userService.deleteById(ids);
        return Result.success();
    }
    @GetMapping("/role/{roleLabel}")
   public Result listByRole(@PathVariable String roleLabel) {
       return Result.success(userService.listByRole(roleLabel));
   }
}
